<template>
  <view class="content">
    <view class="stats-container">
      <view class="stats-card">
        <view class="stats-title"><uni-icons type='home' color='#007AFF' size="24" />今日统计</view>
        <view class="stats-grid">
          <view class="stats-item">
            <text class="stats-label">店铺数</text>
            <text class="stats-value">{{ stats.store_count || 0 }}</text>
          </view>
          <view class="stats-item">
            <text class="stats-label">员工数</text>
            <text class="stats-value">{{ stats.staff_count || 0 }}</text>
          </view>
          <view class="stats-item">
            <text class="stats-label">客户数</text>
            <text class="stats-value">{{ stats.customer_count || 0 }}</text>
          </view>
          <view class="stats-item">
            <text class="stats-label">订单数</text>
            <text class="stats-value">{{ stats.order_count || 0 }}</text>
          </view>
          <view class="stats-item">
            <text class="stats-label">总订单金额</text>
            <text class="stats-value">¥{{ (stats.total_order_amount || 0).toFixed(2) }}</text>
          </view>
        </view>
      </view>
      <view class="stats-card">
        <view class="stats-title">收益分析</view>
        <view class="stats-grid">
          <view class="stats-item">
            <text class="stats-label">充值数</text>
            <text class="stats-value">{{ stats.recharge_count || 0 }}</text>
          </view>
          <view class="stats-item">
            <text class="stats-label">总充值金额</text>
            <text class="stats-value">¥{{ (stats.total_recharge_amount || 0).toFixed(2) }}</text>
          </view>
          <view class="stats-item">
            <text class="stats-label">总成本</text>
            <text class="stats-value">¥{{ (stats.total_cost || 0).toFixed(2) }}</text>
          </view>
          <view class="stats-item">
            <text class="stats-label">净利润</text>
            <text class="stats-value" :style="{ color: netProfitColor }">
              ¥{{ netProfit.toFixed(2) }}
            </text>
          </view>
          <view class="stats-item">
            <text class="stats-label">总提成</text>
            <text class="stats-value">¥{{ (stats.total_commission_cost || 0).toFixed(2) }}</text>
          </view>
        </view>
      </view>
    </view>
    <view class="demo-link" @click="navigateToDemo">
      <text>demo演示</text>
      <uni-icons type="forward" size="24" color="#f01a82" />
    </view>

    <tab-bar />
    
  </view>
</template>

<script>
import auth from '@/utils/auth.js';
import TabBar from '@/components/tab-bar/tab-bar.vue';
import cardConsumptionsAPI from '@/utils/card_consumptions.js';

export default {
  components: {
    TabBar
  },
  computed: {
    netProfit() {
      return (this.stats.total_recharge_amount || 0) - (this.stats.total_cost || 0);
    },
    netProfitColor() {
      return this.netProfit > 0 ? '#4CD964' : this.netProfit < 0 ? '#DD524D' : '#333';
    }
  },
  data() {
    return {
      showCustomNav: true,
      stats: {
        store_count: 0,
        staff_count: 0,
        customer_count: 0,
        order_count: 0,
        total_order_amount: 0,
        recharge_count: 0,
        total_recharge_amount: 0,
        total_cost: 0,
        total_commission_cost: 0
      }
    }
  },
  onLoad() {
    this.checkLogin();
    this.loadStats();  // 添加这行，在页面加载时获取统计数据
  },
  onShow() {
    this.loadStats(); // 添加这行，在页面显示时也刷新数据
  },
  methods: {
    async navigateToDemo() {
      uni.navigateTo({
        url: '/pages/ui/index'
      });
    },
    async checkLogin() {
      if (!auth.getToken()) {
        console.log("--用户未登录--");
        uni.reLaunch({
          url: '/pages/login/index'
        });
        return;
      }

      try {
        const isValid = await auth.valid();
        if (!isValid) {
          console.log("--token已失效--");
          auth.clearTokens();
          uni.reLaunch({
            url: '/pages/login/index'
          });
        } else {
          console.log("--用户已经登录--");
          //this.loadStats();
        }
      } catch (e) {
        console.log("--token验证失败--");
        auth.clearTokens();
        uni.reLaunch({
          url: '/pages/login/index'
        });
      }
    },
    async loadStats() {
      try {
        const res = await cardConsumptionsAPI.getStoreStatistics();
        this.stats = res;
      } catch (e) {
        uni.showToast({
          title: '获取统计数据失败',
          icon: 'none'
        });
      }
    },
    navigateBack() {
      uni.navigateBack();
    }
  }
}
</script>

<style lang="scss">
@import '@/uni.scss';

/* #ifdef H5 */
.content {
  padding: 30rpx;
  background-color: #f5f5f5;
  min-height: 100vh;
}

/* #endif */

/* #ifdef MP-WEIXIN */
.content {
  padding: 30rpx;
  padding-top: 60rpx;
  background-color: #f5f5f5;
  min-height: 100vh;
  margin-top: 60rpx;
}

/* #endif */
.demo-link {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20rpx;
  margin-bottom: 20rpx;
  background: #fff;
  border-radius: 16rpx;
  box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.05);
  color: $uni-primary;
  font-size: 32rpx;
}

.stats-container {
  margin-bottom: 120rpx;
}

.stats-card {
  background-color: #fff;
  border-radius: 16rpx;
  padding: 30rpx;
  margin-bottom: 30rpx;
  box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.05);
}

.stats-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 30rpx;
}

.stats-grid {
  display: flex;
  flex-wrap: wrap;
}

.stats-item {
  flex: 1;
  min-width: 33%;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20rpx 0;
}

.stats-label {
  font-size: 28rpx;
  color: #666;
  margin-bottom: 10rpx;
}

.stats-value {
  font-size: 36rpx;
  color: #333;
  font-weight: bold;
}

/* 确保自定义导航栏在所有平台都显示 */
.custom-nav-bar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 44px;
  background-color: $uni-primary;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 999;
}

/* 添加平台特定样式 */
/* #ifdef H5 */
.custom-nav-bar {
  top: env(safe-area-inset-top);
}

/* #endif */

.container {
  padding-top: 44px;
  padding-bottom: 50px;
}

.search-bar {
  display: flex;
  align-items: center;
  padding: 20rpx;
  background: #fff;
}

.action-buttons {
  display: flex;
  align-items: center;
  margin-left: 20rpx;
}

.search-btn {
  margin-right: 20rpx;
  padding: 0 30rpx;
  height: 60rpx;
  line-height: 60rpx;
  background: linear-gradient(to right, $uni-primary, lighten($uni-primary, 15%));
  color: #fff;
  border-radius: 40rpx;
  border: none;
}
</style>
